package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.mongodb.client.result.DeleteResult;
import com.tanhua.dubbo.server.pojo.UserLike;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service(version = "1.0.0")
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存喜欢
     * @param userId 当前登录用户id
     * @param likeUserId 喜欢的用户ID
     * @return 用户喜欢表的特有id
     */
    @Override
    public String saveUserLike(Long userId, Long likeUserId) {
        //从mongoDb中查询用户喜欢表
        Query query = Query.query(Criteria
                .where("userId")
                .is(userId)
                .and("likeUserId").is(likeUserId));

        //如果能查到,说明已经喜欢了,否则执行下面的操作
        if (this.mongoTemplate.count(query, UserLike.class) > 0) {
            return null;
        }

        UserLike userLike = new UserLike();
        userLike.setId(ObjectId.get());
        userLike.setCreated(System.currentTimeMillis());
        userLike.setUserId(userId);
        userLike.setLikeUserId(likeUserId);

        this.mongoTemplate.save(userLike);
        return userLike.getId().toHexString();
    }

    /**
     * 是否互相喜欢
     * @param userId 当前登录用户id'
     * @param likeUserId 喜欢的用户id
     * @return Boolean
     */
    @Override
    public Boolean isMutualLike(Long userId, Long likeUserId) {
        /*Criteria criteria1 = Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId);
        long count1 = this.mongoTemplate.count(Query.query(criteria1), UserLike.class);
        Criteria criteria2 = Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId);
        long count2 = this.mongoTemplate.count(Query.query(criteria2), UserLike.class);
        return count1==1&&count2==1;*/

        //将用户ID和喜欢的用户id先查一次,将id倒过来再查一次如果结果为2代表两个人互相喜欢
        Criteria criteria1 = Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId);
        Criteria criteria2 = Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId);
        Criteria criteria = new Criteria().orOperator(criteria1, criteria2);
        return this.mongoTemplate.count(Query.query(criteria), UserLike.class) == 2;
    }

    /**
     * 取消喜欢
     * @param userId 用户id
     * @param likeUserId 喜欢的用户id
     * @return Boolean
     */
    @Override
    public Boolean deleteUserLike(Long userId, Long likeUserId) {
        //根据传来的用户ID和喜欢的用户ID删除表
        Query query = Query.query(Criteria
                .where("userId")
                .is(userId)
                .and("likeUserId").is(likeUserId));
        DeleteResult deleteResult = this.mongoTemplate.remove(query, UserLike.class);
        return deleteResult.getDeletedCount() == 1;
    }

    /**
     * 相互喜欢的数量
     * @param userId
     * @return
     */
    @Override
    public Long queryEachLikeCount(Long userId) {
        //先查我喜欢的用户ID添加到集合中
        Query query=Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = this.mongoTemplate.find(query, UserLike.class);

        List<Long> idList=new ArrayList<>();
        for (UserLike userLike : userLikes) {
            idList.add(userLike.getLikeUserId());
        }

        //在查在我喜欢的用户中喜欢我的数量
        Query query1=Query.query(Criteria.where("userId").in(idList).and("likeUserId").is(userId));
        long count = this.mongoTemplate.count(query1, UserLike.class);

        return count;
    }

    /**
     * 喜欢数
     * @param userId
     * @return
     */
    @Override
    public Long queryLikeCount(Long userId) {
        Query query=Query.query(Criteria.where("userId").is(userId));
        long count = this.mongoTemplate.count(query, UserLike.class);

        return count;
    }

    /**
     * 粉丝数
     * @param userId 传来的id
     * @return
     */
    @Override
    public Long queryFanCount(Long userId) {
        Query query=Query.query(Criteria.where("likeUserId").is(userId));
        long count = this.mongoTemplate.count(query, UserLike.class);

        return count;
    }

    /**
     * 查看相互喜欢列表
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<UserLike> queryEachLikeList(Long userId, Integer page, Integer pageSize) {
        //先查我喜欢的用户ID添加到集合中
        Query query=Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = this.mongoTemplate.find(query, UserLike.class);

        List<Long> idList=new ArrayList<>();
        for (UserLike userLike : userLikes) {
            idList.add(userLike.getLikeUserId());
        }

        //在查在我喜欢的用户中喜欢我的列表
        Query query1=Query.query(Criteria.where("userId").in(idList).and("likeUserId").is(userId));
        return getPageInfo(query1, page, pageSize);
    }

    /**
     * 查看我喜欢的列表
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<UserLike> queryLikeList(Long userId, Integer page, Integer pageSize) {
        Query query=Query.query(Criteria.where("userId").is(userId));
        return getPageInfo(query, page, pageSize);
    }

    /**
     * 粉丝列表
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<UserLike> queryFanList(Long userId, Integer page, Integer pageSize) {
        Query query=Query.query(Criteria.where("likeUserId").is(userId));
        return getPageInfo(query, page, pageSize);
    }

    /**
     * 封装pageInfo
     * @param query
     * @param page
     * @param pageSize
     * @return
     */
    private PageInfo<UserLike> getPageInfo(Query query, Integer page, Integer pageSize) {
        //加条件,然后查询
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("created")));
        query.with(pageRequest);
        List<UserLike> list = this.mongoTemplate.find(query, UserLike.class);
        //封装
        PageInfo<UserLike> pageInfo=new PageInfo<>();
        pageInfo.setTotal(0);
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(list);
        return pageInfo;
    }


}
